package org.example.controller;

import com.google.common.collect.ImmutableMap;
import org.example.dto.SubCategoryDto;
import com.google.common.collect.ImmutableMap;
import org.example.dto.DeptDto;
import org.example.entity.SysCategory;
import org.example.entity.SysDept;
import org.example.entity.SysSubCategory;
import org.example.model.PageBean;
import org.example.service.SysSubCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/subCategory")
public class SysSubCategoryController {

    @Resource
    private SysSubCategoryService sysSubCategoryService;

    //根据gridCategoryId获取子分类列表
    @GetMapping("/getList")
    public PageBean<SysSubCategory> getListByGridCategoryId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer size, Integer id){
        return  sysSubCategoryService.getPageData(page, size,id); }

    @PostMapping
    public Map<String, Object> addSubCategory(@RequestBody SubCategoryDto subCategoryDto){

        System.out.println("controller--subCategoryDto--->"+subCategoryDto);

        sysSubCategoryService.addSubCategory(subCategoryDto);
        return ImmutableMap.of("code", 1, "msg", "success"); }

    @GetMapping("/{id}")
    public SysSubCategory getById(@PathVariable("id") Integer id){
        return sysSubCategoryService.getById(id);
    }

    @PutMapping
    public Map<String, Object> editCategory(@RequestBody SubCategoryDto subCategoryDto){
        sysSubCategoryService.editCategory(subCategoryDto);
        return ImmutableMap.of("code", 1, "msg", "success"); }

    @DeleteMapping("/{id}")
    public Map<String, Object> deleteById(@PathVariable("id") Integer id){
        sysSubCategoryService.deleteById(id);
        return ImmutableMap.of("code", 1, "msg", "success"); }

    @GetMapping("/getAll")
    public List<SysSubCategory> getAll() {
        return sysSubCategoryService.getData();
    }

    //获取编辑数据回显
    @GetMapping("/x/{id}")
    public SysSubCategory getById(@PathVariable("id") Long id) {

        System.out.println("edit---->id的值为："+id);
        return sysSubCategoryService.getById(id);
    }

    //编辑数据

    @PutMapping("/edit")
    public Map<String, Object> edit(@RequestBody SubCategoryDto subCategoryDto) {
        System.out.println("controller----->edit---put"+subCategoryDto);
        sysSubCategoryService.edit(subCategoryDto);
        return ImmutableMap.of("code", 1, "msg", "success");
    }
}

